<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜品管理后台列表</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="../../Res/LayUI/css/layui.css" media="all">
<script src="../../Res/JS/jquery-1.10.2.js" charset="utf-8"></script>
<script src="../../Res/JS/date.js" charset="utf-8"></script>
<script type="text/html" id="optime">  

  	{{dateFormat(d.optime)}}
</script>
<script type="text/html" id="foodImg">
	<img src="../../{{(d.foodImg)}}">	
</script>
<link rel="stylesheet" href="../../Res/LayUI/css/modules/treeselect.css">
<!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
	<div class="demoTable">
	  搜索:
	  <div class="layui-inline">
	    <input class="layui-input" name="foodName" id="demoReload" autocomplete="off" placeholder="输入菜品名字">
	  </div>
	  <button class="layui-btn" data-type="reload">搜索</button>
	  <button class="layui-btn" id="foodadd" data-type="add">增加</button>
	</div>
	<table class="layui-hide" id="ehyManager" lay-filter="demo"></table>
 
<script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-xs" lay-event="edit" id="imgUpdate">修改</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
 
<script src="../../Res/LayUI/layui.js"></script>
<script>

layui.use(['laydate', 'laypage', 'layer', 'table', 'upload', 'element'], function(){
	  var laydate = layui.laydate //日期
	  ,laypage = layui.laypage //分页
	  layer = layui.layer //弹层
	  ,table = layui.table //表格
	  ,upload = layui.upload //上传
	  ,element = layui.element; //元素操作
	 
	  
	  //监听Tab切换
	  element.on('tab(demo)', function(data){
	    layer.msg('切换了：'+ this.innerHTML);
	    console.log(data);
	  });
	  
	  //执行一个 table 实例
	  table.render({
	     elem: '#ehyManager'
	    ,width:1000
	    ,height: 490
	    ,url: '../../food/selectfoodAlls' //数据接口
	    ,page: true //开启分页
	    ,method:'post'
	   	,limit: 10
	    ,cols: [[ //表头
	       {type:'checkbox', fixed: 'left'}
	      ,{field: 'foodId', title: '食品awID', width:100,display:'none', fixed: 'left'}
	      ,{field: 'foodTypeName', title: '食品类型名字', width:120}
	      ,{field: 'mealName', title: '餐店名称', width:100}
	      ,{field: 'foodName', title: '食品名称', width:100}
	      ,{field: 'foodPrice', title: '食品价格', width: 100}
	      ,{field: 'foodIntro', title: '食品简介', width: 120}
	      ,{field: 'foodQuantity', title: '食品销量', width: 100}
	      ,{field: 'foodImg', title: '食品图片', width: 120,templet:'#foodImg'}
	      /* ,{field: 'isva', title: '是否有效',display:'none', width: 120}
	      ,{field: 'oper', title: '操作人',display:'none',width: 120} */
	      ,{field: 'optime', title: '操作时间',templet: '#optime',display:'none', width: 120}
	      ,{fixed: 'right',title: '操作', width: 165, align:'center', toolbar: '#barDemo'}
	    ]]
	  	,id: 'testReload'
	  });
	  
	  //监听工具条
	  table.on('tool(demo)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
	    var data = obj.data //获得当前行数据
	    ,layEvent = obj.event; //获得 lay-event 对应的值
	    if(layEvent === 'del'){
	      var foodId=data.foodId;
	      layer.confirm('确认删除？', function(index){
	        layer.close(index);
	        //使用jquery的ajax方式向后台提交数据
			var url = "../../food/delete";
			var data={"foodId":foodId};
				$.post(url,data,function(data){
					if(data=="ok"){
						layer.msg('删除成功', {
							  icon: 1,
							  time: 1000 //1秒关闭（如果不配置，默认是3秒）
							}, function(index, item){
								 location.reload(); // 页面刷新
						});
					}
					
				});
	      	});
	    } else if(layEvent === 'edit'){
	    	var foodId=data.foodId;

	    	var goodsTypeName=data.goodsTypeName;
	        //使用jquery的ajax方式向后台提交数据
			var url = "../../food/findById";
			var data={foodId:foodId};
			$.post(url,data,function(data){
				 layer.open({
					  type: 2,
					  area : ['60%' , '90%'],//弹出框大小
					  shift:1,//弹出框动画效果
					  content: ['foodupdate.html','on'], //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
				 	  success:function(layero,index){
				 		  var body = layer.getChildFrame('body',index);//确定两个页面之间的父子关系
				 		  var iframeWin = window[layero.find('iframe')[0]['name']];//子页面对象
				 		  console.log(body.html()) //得到iframe页的body内容
				 		  body.find("#foodId").val(data.foodId);
				 		  body.find("#foodTypeId").val(data.foodTypeId);
				 		  body.find("#mealId").val(data.mealId);
				 		  body.find("#foodName").val(data.foodName);
				 		  body.find("#foodPrice").val(data.foodPrice);
				 		  body.find("#foodIntro").val(data.foodIntro);
				 		  body.find("#foodQuantity").val(data.foodQuantity);
				 		  body.find("#foodImg").val(data.foodImg);
				 		  body.find("#isva").val(data.isva);
				 		  body.find("#optime").val(data.optime);
				 		  body.find("#oper").val(data.oper);
				 	  }			 
					}); 	
				/* 
			 eval("var ehyManagerVo="+data); */
				   
			},dataType="JSON");
	    }
	  });
	  
	  //条件查询
	  var $ = layui.$;
	  $('.demoTable .layui-btn').on('click', function(){
		 var demoReload = $('#demoReload');
		 //执行重载
	     table.reload('testReload', {
	       page: {
	         curr: 1 //重新从第 1 页开始
	       }
	       ,where: {
	    	   foodName: demoReload.val()
	       }
	     });
	  });
	  $('#foodadd').on('click', function(){
		 layer.open({
			  type: 	2,
			  area : ['60%' , '80%'],//弹出框大小
			  shift:1,//弹出框动画效果
			  content: ['foodadd.html','on'] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
			}); 
	   }); 
	});
</script>
</body>
</html>